Skip to content

Conversation

@suddendust
Copy link
Contributor

@suddendust suddendust commented Jan 2, 2026

Fix Postgres cannot extract elements from a scalar error for JSON null arrays

What was breaking?

jsonb_array_elements() throws PSQLException: cannot extract elements from a scalar when encountering JSON null values in JSONB array fields. This affected both UnnestExpression queries and ArrayRelationalFilterExpression.ANY filters.

How was the bug reproduced?

Added test data with props.colors: null (JSON null, not SQL NULL) and ran queries that unnest or filter on that field.

What was the fix applied?

Replaced direct jsonb_array_elements(field) calls with:

jsonb_array_elements(CASE WHEN jsonb_typeof(field) = 'array' THEN field ELSE '[]'::jsonb END)

The new query looks like:

With 
table0 as (SELECT * from "entities_api"),
table1 as (SELECT * from table0 t0 LEFT JOIN LATERAL jsonb_array_elements(CASE WHEN jsonb_typeof("customAttribute"->'dev-ops-owner') = 'array' THEN "customAttribute"->'dev-ops-owner' ELSE '[]'::jsonb END) p1("customAttribute_dot_dev-ops-owner") on TRUE)
SELECT "customAttribute_dot_dev-ops-owner" AS "dev-ops-owner", COUNT(DISTINCT "id" ) AS "DISTINCTCOUNT_id" FROM table1 WHERE "tenantId" = ? AND "tenantId" = ? GROUP BY "customAttribute_dot_dev-ops-owner" OFFSET ? LIMIT ?

Testing

[X] Added integration tests

Checklist:

  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Any dependent changes have been merged and published in downstream modules

@codecov
Copy link

codecov bot commented Jan 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.28%. Comparing base (9b4ec54) to head (a21f23d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##               main     #265   +/-   ##
=========================================
  Coverage     80.28%   80.28%           
  Complexity     1341     1341           
=========================================
  Files           231      231           
  Lines          6091     6093    +2     
  Branches        547      548    +1     
=========================================
+ Hits           4890     4892    +2     
  Misses          826      826           
  Partials        375      375           
Flag Coverage Δ
integration 80.28% <100.00%> (+<0.01%) ⬆️
unit 58.08% <66.66%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

suresh-prakash
suresh-prakash previously approved these changes Jan 2, 2026
@suresh-prakash suresh-prakash merged commit a7acef1 into hypertrace:main Jan 6, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants